package com.wing.productionPlan.controller.app;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.utils.JsonResult;
import com.wing.web.utils.LocaleMessage;
import com.wing.common.utils.PageResult;
import com.wing.productionPlan.model.entity.ProductionProcessMaterial;
import com.wing.productionPlan.model.response.ProductionProcessMaterialVO;
import com.wing.productionPlan.service.ProductionProcessMaterialService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.wing.productionPlan.model.request.ProductionProcessMaterialForm;
import org.springframework.validation.annotation.Validated;

import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author
 */
@Validated
@Api(value = "App-生产工序物料表管理", tags = "生产工序物料表管理")
@RestController("appProductionProcessMaterialController")
@RequestMapping("productionPlan/app/productionProcessMaterial")
public class ProductionProcessMaterialController {

    @Autowired
    private ProductionProcessMaterialService productionProcessMaterialService;
    @Autowired
    private LocaleMessage localeMessage;

    @ApiOperation(value = "工序物料(工单)列表", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
    })
    @GetMapping("/list")
    public PageResult<ProductionProcessMaterialVO> list(
            @RequestParam(name = "page", defaultValue = "1", required = false) Integer page
            ,@RequestParam(name = "limit", defaultValue = "10", required = false) Integer limit
            ,@RequestParam(name = "sort", defaultValue = "create_time", required = false) String sort
            ,@RequestParam(name = "order", defaultValue = "desc", required = false) String order
            ,Long simpleOrderProcessId
            ,Long simpleOrderId
    ) {
        Page<ProductionProcessMaterialVO> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<>();

        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            params.put("sort", sort);
            params.put("order", order);
        }
        params.put("processId",simpleOrderProcessId);
        params.put("simpleWorkOrderId",simpleOrderId);

        Page<ProductionProcessMaterialVO> list = productionProcessMaterialService.pageList4app(pg, params);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @GetMapping("/{id}")
    public JsonResult detail(@PathVariable("id") Long productionProcessMaterialId) {
        ProductionProcessMaterialVO productionProcessMaterialVO = productionProcessMaterialService.get4appById(productionProcessMaterialId);
        if (productionProcessMaterialVO == null) {
            return JsonResult.fail(localeMessage.getMessage("common.fail"));
        }
        return JsonResult.success(localeMessage.getMessage("common.success"), productionProcessMaterialVO);
    }

    @PostMapping()
    public JsonResult add(@Validated ProductionProcessMaterialForm productionProcessMaterialForm) {
        if (productionProcessMaterialService.add(productionProcessMaterialForm) > 0) {
            return JsonResult.success(localeMessage.getMessage("common.success"));
        }
        return JsonResult.fail(localeMessage.getMessage("common.fail"));
    }

}
